package cn.controller;

import cn.common.CsbitException;
import cn.common.Helpers;
import cn.model.ApuConfig;
import cn.model.ApuNums;
import cn.model.Person;
import cn.model.WarnConfig;
import cn.vo.SearchVO;
import cn.web.BaseService;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.MarkType;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.data.PointData;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.series.Bar;
import com.google.common.collect.HashBiMap;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.SerializationUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.*;
import java.util.*;

/**
 * Created by wushifeng on 2017/3/14.
 */
@Controller
@RequestMapping("/form")
@Slf4j
public class FormController {

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index() {
        log.info("jrebel reload");
        return "vform";
    }

    @RequestMapping(value = "save", method = RequestMethod.POST, produces = {org.springframework.http.MediaType.TEXT_PLAIN_VALUE})
    @ResponseBody
    public String saveApu(@RequestParam(name = "id") Integer id, @RequestBody @Valid ApuConfig apuCfg,
                          BindingResult bindingResult) {
        Helpers.handleBindingResult(bindingResult);
        return Helpers.SUCCESS;
    }

    @RequestMapping(value = "get", method = RequestMethod.GET)
    @ResponseBody
    public Object getApu(){
        ApuConfig apuConfig = new ApuConfig();
        apuConfig.setVersionid(1);
        Map<ApuConfig.ApuType, ApuNums> apuCfg = new HashMap<>();

        for (ApuConfig.ApuType e : ApuConfig.ApuType.values()) {
            ApuNums nums = new ApuNums();
            nums.setNumH(12);
            nums.setNumS(1);
            nums.setNumN(50);
            apuCfg.put(e, nums);
        }
        apuConfig.setApuCfg(apuCfg);

        //return new HashMap(){{
            //put("apu", apuConfig);//SerializationUtils.serialize(apuConfig));
        //}};
        //throw new CsbitException() //HandlerExceptionResolver会截获进行统一
        return apuConfig;
    }

    //https://github.com/abel533/ECharts 这个是baidu echarts服务端的封装，其test有各种例子
    @RequestMapping(value = "chart", method = RequestMethod.GET)
    @ResponseBody
    public Object chart(){
        Option option = new Option();
        option.title().text("某地区蒸发量和降水量").subtext("纯属虚构");
        option.tooltip().trigger(Trigger.axis);
        option.legend("蒸发量", "降水量");
        option.toolbox().show(true).feature(Tool.mark, Tool.dataView, new MagicType(Magic.line, Magic.bar).show(true), Tool.restore, Tool.saveAsImage);
        option.calculable(true);
        option.xAxis(new CategoryAxis().data("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"));
        option.yAxis(new ValueAxis());

        Bar bar = new Bar("蒸发量");
        bar.data(2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3);
        bar.markPoint().data(new PointData().type(MarkType.max).name("最大值"), new PointData().type(MarkType.min).name("最小值"));
        bar.markLine().data(new PointData().type(MarkType.average).name("平均值"));

        Bar bar2 = new Bar("降水量");
        List<Double> list = Arrays.asList(2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3);
        bar2.setData(list);
        bar2.markPoint().data(new PointData("年最高", 182.2).xAxis(7).yAxis(183).symbolSize(18), new PointData("年最低", 2.3).xAxis(11).yAxis(3));
        bar2.markLine().data(new PointData().type(MarkType.average).name("平均值"));

        option.series(bar, bar2);
        return option;
    }

    @RequestMapping(value = "log", method = RequestMethod.GET)
    @ResponseBody
    public String log(HttpServletRequest req, HttpServletResponse resp, String name){
        return name;
    }

    @Autowired
    BaseService svc;

    @RequestMapping(value = "warn", method = RequestMethod.GET)
    @ResponseBody
    public List<WarnConfig> warn(){
        return this.svc.getAll(WarnConfig.class);
    }

    @RequestMapping(value = "conv", method = RequestMethod.POST)
    @ResponseBody
    public SearchVO conv(@RequestBody @Valid SearchVO v){
        ModelMapper modelMapper = new ModelMapper();
       modelMapper.addMappings(new PropertyMap<SearchVO, WarnConfig>() {
            protected void configure() {
                map().setName(source.getFullName());
                //map().address.setCity(source.city);
            }
        });
        WarnConfig w = modelMapper.map(v, WarnConfig.class);
        //this.svc.save(w);
        return v;
    }

    //使用kotlin简化代码的编写 官网地址 http://kotlinlang.org/docs/reference/
    //https://github.com/Kotlin/kotlin-koans 快速掌握的例子
    @RequestMapping(value = "kt", method = RequestMethod.GET)
    @ResponseBody
    public  cn.model.Customer kt(){
        //这种场景可以简化代码
        //选择注意：kotlin-stdlib-1.1.1.jar 868K
        cn.model.Customer c = new cn.model.Customer(1, "wu", "beijing");
        String kt = "ssss";
        cn.kt.ExtensionKt.showFirstCharacter(kt);
        cn.kt.ExtensionKt.lastChar(kt);

        return c;
    }
}
